﻿using System;

using Wicresoft.Framework.Organization;
using MaLu.BusiLogic;
using MaLu.Common;
using System.Data;
using MaLu.Data;
using System.Xml;
using MaLu.Framework.Common;


namespace MaluJg.web.Visite
{
    public partial class item2w :VisitePage
    {
        public Guid DepartmentID
        {
            get
            {
                if (Request["DepartmentID"] != null)
                    return new Guid(Request["DepartmentID"]);
                return Guid.NewGuid();
            }
        }



        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {


                if (VisiteID != Guid.Empty)
                {

                    DataTable dt = SqlHelper.Instance.ExecuteDataTable(Provider, "select * from Visite where id ='" + VisiteID.ToString() + "'");
                    if (dt.Rows.Count > 0)
                    {
                        lbLeader.Text = dt.Rows[0]["Leader"].ToString();
                        lbDept.Text = dt.Rows[0]["Interviewee"].ToString();
                        lbYear.Text = dt.Rows[0]["Year"].ToString();
                        lbMonth.Text = dt.Rows[0]["Month"].ToString();

                    }
                }
                BindData();
            }
        }
        private void BindData()
        {
            string projectname = string.Empty;
            string content = BLVisite.GetVisiteItem(Provider, VisiteID, "item2.aspx", ref projectname);
            if (content != string.Empty)
            {
                XmlDocument document = new XmlDocument();
                document.LoadXml(content);
                XmlElement xe = document.SelectSingleNode("//记录人") as XmlElement;
                tbRecord.Text = Util.ReturnXml(xe.InnerText);
                xe = document.SelectSingleNode("//走访领导签字") as XmlElement;
                tbSign.Text = Util.ReturnXml(xe.InnerText);
                xe = document.SelectSingleNode("//村居委负责人签字") as XmlElement;
                tbFuze.Text = Util.ReturnXml(xe.InnerText);
                xe = document.SelectSingleNode("//问题") as XmlElement;

                XmlNodeList xnl = xe.ChildNodes;
                if (xnl.Count >= 1)
                    tbPro0.Text = xnl[0].InnerText;
                if (xnl.Count >= 2)
                    tbPro1.Text = xnl[1].InnerText;
                if (xnl.Count >= 3)
                    tbPro2.Text = xnl[2].InnerText;
                if (xnl.Count >= 4)
                    tbPro3.Text = xnl[3].InnerText;
                if (xnl.Count >= 5)
                    tbPro4.Text = xnl[4].InnerText;
                if (xnl.Count >= 6)
                    tbPro5.Text = xnl[5].InnerText;
                if (xnl.Count >=7)
                    tbPro6.Text = xnl[6].InnerText;

                if (xnl.Count >= 8)
                    tb0.Text = xnl[7].InnerText;
                if (xnl.Count >=9)
                    tb1.Text = xnl[8].InnerText;
                if (xnl.Count >= 10)
                    tb2.Text = xnl[9].InnerText;
                if (xnl.Count >= 11)
                    tb3.Text = xnl[10].InnerText;
                if (xnl.Count >= 12)
                    tb4.Text = xnl[11].InnerText;
                if (xnl.Count >= 13)
                    tb5.Text = xnl[12].InnerText;
                if (xnl.Count >= 14)
                    tb6.Text = xnl[13].InnerText;

            }
        }

        private string FillContent()
        {
            string template = BLVisite.GetTemplate(Provider, "item2.aspx");
            if (template != string.Empty)
            {

                XmlDocument document = new XmlDocument();
                document.LoadXml(template);
                XmlElement xe = document.SelectSingleNode("//记录人") as XmlElement;
                xe.InnerText = Util.ReplaceXml(tbRecord.Text.Trim());
                xe = document.SelectSingleNode("//走访领导签字") as XmlElement;
                xe.InnerText = Util.ReplaceXml(tbSign.Text.Trim());
                xe = document.SelectSingleNode("//村居委负责人签字") as XmlElement;
                xe.InnerText = Util.ReplaceXml(tbFuze.Text.Trim());
                xe = document.SelectSingleNode("//问题") as XmlElement;
                XmlNode xn = document.CreateElement("p");
                xn.InnerText = string.Format("{0}", Util.ReturnXml(tbPro0.Text));
                xe.AppendChild(xn);
                xn = document.CreateElement("p");
                xn.InnerText = string.Format("{0}", Util.ReturnXml(tbPro1.Text));
                xe.AppendChild(xn);
                xn = document.CreateElement("p");
                xn.InnerText = string.Format("{0}", Util.ReturnXml(tbPro2.Text));
                xe.AppendChild(xn);
                xn = document.CreateElement("p");
                xn.InnerText = string.Format("{0}", Util.ReturnXml(tbPro3.Text));
                xe.AppendChild(xn);
                xn = document.CreateElement("p");
                xn.InnerText = string.Format("{0}", Util.ReturnXml(tbPro4.Text));
                xe.AppendChild(xn);
                xn = document.CreateElement("p");
                xn.InnerText = string.Format("{0}", Util.ReturnXml(tbPro5.Text));
                xe.AppendChild(xn);
                xn = document.CreateElement("p");
                xn.InnerText = string.Format("{0}", Util.ReturnXml(tbPro6.Text));
                xe.AppendChild(xn);

                xn = document.CreateElement("t");
                xn.InnerText = string.Format("{0}", Util.ReturnXml(tb0.Text));
                xe.AppendChild(xn);
                xn = document.CreateElement("t");
                xn.InnerText = string.Format("{0}", Util.ReturnXml(tb1.Text));
                xe.AppendChild(xn);
                xn = document.CreateElement("t");
                xn.InnerText = string.Format("{0}", Util.ReturnXml(tb2.Text));
                xe.AppendChild(xn);
                xn = document.CreateElement("t");
                xn.InnerText = string.Format("{0}", Util.ReturnXml(tb3.Text));
                xe.AppendChild(xn);
                xn = document.CreateElement("t");
                xn.InnerText = string.Format("{0}", Util.ReturnXml(tb4.Text));
                xe.AppendChild(xn);
                xn = document.CreateElement("t");
                xn.InnerText = string.Format("{0}", Util.ReturnXml(tb5.Text));
                xe.AppendChild(xn);
                xn = document.CreateElement("t");
                xn.InnerText = string.Format("{0}", Util.ReturnXml(tb6.Text));
                xe.AppendChild(xn);
                return document.OuterXml;
            }
            else
                return string.Empty;
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            BLVisite.SaveVisiteItem(Provider, VisiteID, "item2.aspx", FillContent());


            string msg = "已保存!";

            string script = "<script>alert('" + msg + "');</script>";
            ClientScript.RegisterStartupScript(this.GetType(), "Close", script);


        }
    }
}
